package com.dishdex.menuhub.module.module.user.service;

import com.dishdex.menuhub.module.module.user.entity.User;
import com.dishdex.menuhub.module.module.user.mapper.UserMapper;
import com.dishdex.menuhub.module.utils.BaseUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName:
 * Package: com.dishdex.menuhub.module.module.user.service
 * Description:
 *
 * @Author:
 * @Create 2025/10/22 9:51
 * @Version 1.0
 */
@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public User getById(Long userId) {
        if (BaseUtils.isEmpty(userId)) {
            throw new RuntimeException("id不能为空");
        }
        User user = userMapper.getById(userId);
        if (BaseUtils.isEmpty(user)) {
            throw new RuntimeException("用户不存在");
        }
        return user;
    }

    public User extractById(Long userId) {
        if (BaseUtils.isEmpty(userId)) {
            throw new RuntimeException("id不能为空");
        }
        User user = userMapper.extractById(userId);
        if (BaseUtils.isEmpty(user)) {
            throw new RuntimeException("用户不存在");
        }
        return user;
    }

    public Long edit(Long userId,
                     String avatar,
                     String phone,
                     String password,
                     String name) {
        User user = new User()
                .setAvatar(avatar)
                .setPhone(phone)
                .setName(name)
                .setUpdateTime(BaseUtils.currentSeconds());

        try {
            user.setPassword(BaseUtils.md5(password));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        if (userId == null) {
            if (BaseUtils.isEmpty(phone)) {
                throw new RuntimeException("phone不能为空");
            }
            if (BaseUtils.isEmpty(password)) {
                throw new RuntimeException("password不能为空");
            }
            user.setCreateTime(BaseUtils.currentSeconds());
            user.setIsDeleted(0);
            userMapper.insert(user);
            return user.getId();
        } else {
            if (BaseUtils.isEmpty(userId)) {
                throw new RuntimeException("userId不能为空");
            }
            User temp = userMapper.getById(userId);
            if (BaseUtils.isEmpty(temp)) {
                throw new RuntimeException("用户不存在");
            }
            user.setId(userId);
            userMapper.update(user);
            return user.getId();
        }
    }

    public int delete(Long userId) {
        if (BaseUtils.isEmpty(userId)) {
            throw new RuntimeException("userId不能为空");
        }
        User user = userMapper.getById(userId);
        if (BaseUtils.isEmpty(user)) {
            throw new RuntimeException("用户不存在");
        }
        return userMapper.delete(userId);
    }

    public List<User> getAll() {
        return userMapper.getAll();
    }

    public User getByPhone(String phone) {
        if (BaseUtils.isEmpty(phone)) {
            throw new RuntimeException("phone不能为空");
        }
        User user = userMapper.getByPhone(phone);
        if (BaseUtils.isEmpty(user)) {
            throw new RuntimeException("用户不存在");
        }
        return user;
    }

    public Boolean login(String phone, String password) {
        User user = getByPhone(phone);
        try {
            return BaseUtils.md5(password).equals(user.getPassword());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

